import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'login_controller.dart';

/// 登录页面
class LoginPage extends StatelessWidget {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LoginController>(
      init: LoginController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: context.theme.scaffoldBackgroundColor,
          body: SafeArea(
            child: SingleChildScrollView(
              padding: EdgeInsets.symmetric(horizontal: 24.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  SizedBox(height: 80.h),

                  // Logo 和标题
                  _buildHeader(context),

                  SizedBox(height: 60.h),

                  // 登录表单
                  _buildLoginForm(context, controller),

                  SizedBox(height: 40.h),

                  // 登录按钮
                  _buildLoginButton(context, controller),

                  SizedBox(height: 20.h),

                  // 其他操作
                  _buildOtherActions(context, controller),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  /// 构建头部
  Widget _buildHeader(BuildContext context) {
    return Column(
      children: [
        // Logo
        Container(
          width: 80.w,
          height: 80.w,
          decoration: BoxDecoration(
            color: context.theme.primaryColor,
            borderRadius: BorderRadius.circular(16.r),
          ),
          child: Icon(
            Icons.medical_services,
            size: 40.w,
            color: Colors.white,
          ),
        ),

        SizedBox(height: 24.h),

        // 标题
        Text(
          '医疗管理系统',
          style: TextStyle(
            fontSize: 28.sp,
            fontWeight: FontWeight.bold,
            color: context.theme.textTheme.titleLarge?.color,
          ),
        ),

        SizedBox(height: 8.h),

        Text(
          '请登录您的账户',
          style: TextStyle(
            fontSize: 16.sp,
            color: context.theme.textTheme.bodyMedium?.color,
          ),
        ),
      ],
    );
  }

  /// 构建登录表单
  Widget _buildLoginForm(BuildContext context, LoginController controller) {
    return Column(
      children: [
        // 用户名输入框
        TextFormField(
          controller: controller.usernameController,
          decoration: InputDecoration(
            labelText: '用户名',
            hintText: '请输入用户名',
            prefixIcon: const Icon(Icons.person_outline),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(12.r),
            ),
            filled: true,
            fillColor: context.theme.cardColor,
          ),
          keyboardType: TextInputType.text,
          textInputAction: TextInputAction.next,
        ),

        SizedBox(height: 16.h),

        // 密码输入框
        Obx(() => TextFormField(
              controller: controller.passwordController,
              obscureText: controller.isPasswordHidden.value,
              decoration: InputDecoration(
                labelText: '密码',
                hintText: '请输入密码',
                prefixIcon: const Icon(Icons.lock_outline),
                suffixIcon: IconButton(
                  icon: Icon(
                    controller.isPasswordHidden.value
                        ? Icons.visibility_off
                        : Icons.visibility,
                  ),
                  onPressed: controller.togglePasswordVisibility,
                ),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12.r),
                ),
                filled: true,
                fillColor: context.theme.cardColor,
              ),
              textInputAction: TextInputAction.done,
              onFieldSubmitted: (_) => controller.onLoginTap(),
            )),

        SizedBox(height: 16.h),

        // 记住密码和忘记密码
        Row(
          children: [
            Obx(() => Checkbox(
                  value: controller.isRememberPassword.value,
                  onChanged: controller.toggleRememberPassword,
                )),
            Text(
              '记住密码',
              style: TextStyle(
                fontSize: 14.sp,
                color: context.theme.textTheme.bodyMedium?.color,
              ),
            ),
            const Spacer(),
            TextButton(
              onPressed: controller.onForgotPasswordTap,
              child: Text(
                '忘记密码？',
                style: TextStyle(
                  fontSize: 14.sp,
                  color: context.theme.primaryColor,
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  /// 构建登录按钮
  Widget _buildLoginButton(BuildContext context, LoginController controller) {
    return Obx(() => ElevatedButton(
          onPressed: controller.isLoading.value ? null : controller.onLoginTap,
          style: ElevatedButton.styleFrom(
            backgroundColor: context.theme.primaryColor,
            foregroundColor: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 16.h),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12.r),
            ),
            elevation: 2,
          ),
          child: controller.isLoading.value
              ? SizedBox(
                  width: 20.w,
                  height: 20.w,
                  child: const CircularProgressIndicator(
                    strokeWidth: 2,
                    valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                  ),
                )
              : Text(
                  '登录',
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w600,
                  ),
                ),
        ));
  }

  /// 构建其他操作
  Widget _buildOtherActions(BuildContext context, LoginController controller) {
    return Column(
      children: [
        // 注册账户
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '还没有账户？',
              style: TextStyle(
                fontSize: 14.sp,
                color: context.theme.textTheme.bodyMedium?.color,
              ),
            ),
            TextButton(
              onPressed: controller.onRegisterTap,
              child: Text(
                '立即注册',
                style: TextStyle(
                  fontSize: 14.sp,
                  color: context.theme.primaryColor,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ],
        ),

        SizedBox(height: 20.h),

        // 分割线
        Row(
          children: [
            Expanded(
              child: Divider(
                color: context.theme.dividerColor,
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Text(
                '或',
                style: TextStyle(
                  fontSize: 14.sp,
                  color: context.theme.textTheme.bodyMedium?.color,
                ),
              ),
            ),
            Expanded(
              child: Divider(
                color: context.theme.dividerColor,
              ),
            ),
          ],
        ),

        SizedBox(height: 20.h),

        // 游客登录
        OutlinedButton(
          onPressed: controller.onGuestLoginTap,
          style: OutlinedButton.styleFrom(
            foregroundColor: context.theme.primaryColor,
            side: BorderSide(
              color: context.theme.primaryColor,
              width: 1,
            ),
            padding: EdgeInsets.symmetric(vertical: 12.h),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12.r),
            ),
          ),
          child: Text(
            '游客登录',
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ],
    );
  }
}
